package fixtures;

import java.awt.Component;
import java.io.IOException;
import librarianPanels.BorrowBookPanel;
import librarianPanels.ReturnABookPanel;

import clientGUI.LoginPanel;
import clientGUI.MainGUI;
import clientGUI.SubscriberGUI;
import subscriberPanels.ExtendBorrowPanel;
import fit.ActionFixture;

public class ExtendBorrow extends ActionFixture 
{
	MainGUI main;
	LoginPanel lg;
	ExtendBorrowPanel ebp;
	String userName;
	String pw;
	SubscriberGUI sg;
	String check;
	String bid;
	
	String newPassword;
	String oldPassword;
	

	public void startConnection()
	{
			lg=new LoginPanel();
			lg.textFieldLogin.setText("-1");
			String temp=lg.textFieldLogin.getText();
			lg.passwordField.setText("12345678");
			MainGUI.done=false;
			lg.btnSubmit.doClick();		
			while(MainGUI.done==false);
			Component x[]=MainGUI.panelButtons.getComponents();
			int y=MainGUI.panelChanging.getComponentCount();
			MainGUI.panelButtons.toString();
			MainGUI.done=false;
			sg=((SubscriberGUI)x[0]);	
			sg.btnExtendBorrow.doClick();
			while(MainGUI.done==false);
			ebp=(ExtendBorrowPanel)MainGUI.panelChanging.getComponent(0);
	}
	
	
	
	public void popularOrRegular(String choice)
	{
		if(choice.compareTo("popular")==0)
		{
			inputBorrow("1");
			bid="1";
		}
		else
		{
			inputBorrow("8");
			bid="8";
		}
	}
	
	
	
	
	
	
	
	
	
	
	public void logout() throws IOException {
		sg.btnLogout.doClick();
		while(MainGUI.done==false);
	}
	
	
	public void delete()
	{
		String[] msg={"DeleteTestUsers"};
		main.OBLClient.handleMessageFromClient(msg);
	}
	
	
	public void inputBorrow(String bid)
	{
		BorrowBookPanel bbp=new BorrowBookPanel();
		bbp.textFieldBorrowBookID.setText(bid);
		bbp.textFieldCopy.setText("1");
		bbp.textFieldSubsID.setText("-1");
		bbp.btnSubmit.doClick();
		while(MainGUI.done==false);
	}


	public void initGui() throws IOException
	{
		main=new MainGUI("localhost",5555);
		MainGUI.testmode=true;
		createReaderCard();
	}
	
	
	
	
	public boolean checkExtendNotWeekBefore()
	{
		try {
			Thread.sleep(100);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ebp.table.setRowSelectionInterval(0,0);
		String beforeExtend=(String)ebp.table.getValueAt(ebp.table.getSelectedRow(), 2);
		MainGUI.done=false;
		ebp.btnExtend.doClick();
		String afterExtend=(String)ebp.table.getValueAt(ebp.table.getSelectedRow(), 2);
		returnBook(bid);
		try 
		{
			logout();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		if(beforeExtend.compareTo(afterExtend)==0) 
			return true;
		return false;	
	}
	
	
	
	public boolean checkExtend()
	{
		try {
			Thread.sleep(100);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ebp.table.setRowSelectionInterval(0,0);
		String beforeExtend=(String)ebp.table.getValueAt(ebp.table.getSelectedRow(), 2);
		MainGUI.done=false;
		ebp.btnExtend.doClick();
		while(MainGUI.done==false);
		String afterExtend=(String)ebp.table.getValueAt(ebp.table.getSelectedRow(), 2);
		returnBook(bid);
		try 
		{
			logout();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		
		if(beforeExtend.compareTo(afterExtend)==0) 
			return true;
		return false;	
	}
	
	
	
	
	
	
	public void returnBook(String bid)
	{
		ReturnABookPanel rbp=new ReturnABookPanel();
		rbp.textFieldCopy.setText("1");
		rbp.textFieldReturnBookID.setText(bid);
		MainGUI.done=false;
		rbp.btnSubmit.doClick();
		while(MainGUI.done==false);
	}
	
	
	public void createReaderCard()
	{
		String[] s=new String[6];
		s[0]="CreateNewReaderCard";
		s[1]="-1"; 
		s[2]="cat";
		s[3]="dog";
		s[4]="0528512932";
		s[5]="stam@stam.com";
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}

	
}
